package PairedReadFlagStat;
use lib "$ENV{MYSQL_DRAW}/lib";
use MyTools;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;
 
@ISA = qw(Exporter AutoLoader);

@EXPORT = qw( readPairedReadFlagStat );

use Data::Dumper;

sub readPairedReadFlagStat {
	my ($dbh, $prefix, $modifier, $field_list) = @_;
	open(FIN, "$prefix.flagstat") || die "cannot open $prefix.flagstat\n";
	my %hash;
	while (<FIN>) {
		my ($val) = split(/ /, $_);
		$hash{$_} = $val;
	}
	close FIN;
	my $RV;
	foreach my $fd (@{$field_list}) {
		my $metric_id = MyTools::fetchrow1col1_arrayref($dbh, "select MIID from MetricInfo where ", {'FieldName' => $fd, 'Modifier' => $modifier}) or die "Field $fd $modifier not found\n";
#		printf STDERR "$fd\t$metric_id\n";
		my $fd_val = "NA";
		foreach my $line (keys %hash) {
			if ($line =~ /$fd/) {
				$fd_val = $hash{$line};
				last;
			}
		}
		$RV->{$metric_id}->{'value'} = $fd_val;
		$RV->{$metric_id}->{'field_name'} = $fd;
	}
	return $RV;
}
1;

